<?php
namespace Home\Controller;
use Think\Controller;
class RespondController extends Controller{

	//异步接受回调结果
  public function wxNotify(){ 
    Vendor("Wxpay.JsApiPay");
    Vendor("Wxpay.WxPayApi");

    $response = $GLOBALS["HTTP_RAW_POST_DATA"]?$GLOBALS["HTTP_RAW_POST_DATA"]:$_POST;
    if(!isset($_POST['result_code'])){
      $result = \WxPayResults::Init($response);    
    } else {  
      $result = \WxPayResults::InitFromArray($response,true);
    }

    //签名错误-- 非法操作
    if(empty($result['sign'])){
      $result = array(); 
      $result['return_code'] = "FAIL";
      $result['return_msg'] = "签名错误,非法操作";
    }

    if($result['result_code'] == "SUCCESS"){
      $openid = $result['openid'];
      $total_fee = $result['total_fee'] / 100;
      $out_trade_no = $_POST['out_trade_no'];
      $transaction_id = $result['transaction_id'];//微信订单号

      D('Pay')->order_paid($out_trade_no, $total_fee, 1, $transaction_id, $openid);
      
      $returndata['return_code'] = 'SUCCESS';
    } else if ($result['return_code']=="SUCCESS"){
      $returndata['return_code'] = 'FAIL';
      $returndata['return_msg'] = '签名失败';
    } else{
      $returndata['return_code'] = 'FAIL';
      $returndata['return_msg'] = '无数据返回';
    }
    $xml = array_toXml($returndata);
    echo $xml;
    exit;
  }

 
   

}
?>

